package com.FileChar;

import java.io.*;

/**
 * @author xiyu
 * @version 1.0
 * @date 2025/4/8 9:26
 * @description
 */
public class FileCharDemo {
    //字符输入 读取
    public static void fileRead(){
        try {
            FileReader fr = new FileReader("d:\\file\\test\\0001.txt");
            char[] cs = new char[256];
            fr.read(cs);

            System.out.println(new String(cs));
            fr.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //字符输出 写入
    public static void fileWrite(){
        try {
            FileWriter fw = new FileWriter("d:\\file\\test\\0003.txt");
            fw.write("hello,world");
            //强制刷新
            fw.flush();
            fw.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //基于字符的复制粘贴
    public static void copyfile(){
        try {
            FileReader fr = new FileReader("d:\\file\\test\\0001.txt");
            FileWriter fw = new FileWriter("d:\\file\\test\\0003.txt");

            char[] chars = new char[256];
            int len = 0;
            while ((len = fr.read(chars))!=-1){

                fw.write(chars,0,len);
                fw.flush();
            }

            fw.close();
            fr.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static void printWriter(){
        try {
            PrintWriter pw = new PrintWriter("d:\\file\\test\\0003.txt");
            pw.println("鹅鹅鹅");
            pw.println("qxxtg");
            pw.println("bmfls");
            pw.println("hzbqb");
            pw.flush();
            pw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
//        fileRead();
//        fileWrite();
//        copyfile();
        printWriter();
    }
}
